package service

import (
	"RocFamilySystem/internal/repository"
	"RocFamilySystem/models"
	"RocFamilySystem/tools/bcrypt"
)

type SysUserService struct {
	SysUserRepository *repository.SysUserRepository
}

// 初始化
func init() {
	registerContainer["SysUserService"] = &SysUserService{}
}

// SetRepository 设置持久层
func (service *SysUserService) SetRepository(sysUserRepository *repository.SysUserRepository) {
	service.SysUserRepository = sysUserRepository
}

// AddSysUser 添加系统用户
func (service *SysUserService) AddSysUser(sysUser *models.SysUser) {

	//密码加密
	*sysUser.Password = bcrypt.EncodePassword(*sysUser.Password)

	//添加用户
	service.SysUserRepository.InsertSysUser(sysUser)
}

// RemoveSysUser 删除系统用户
func (service *SysUserService) RemoveSysUser(sysUserId int) {
	service.SysUserRepository.DeleteSysUser(sysUserId)
}

// RemoveSysUserBatch 批量删除系统用户
func (service *SysUserService) RemoveSysUserBatch(ids []string) {
	service.SysUserRepository.DeleteSysUserBatch(ids)
}

// ModifySysUser 修改系统用户
func (service *SysUserService) ModifySysUser(sysUser *models.SysUser) {
	//密码加密
	*sysUser.Password = bcrypt.EncodePassword(*sysUser.Password)
	service.SysUserRepository.UpdateSysUser(sysUser)
}

// QuerySysUser 查询系统用户
func (service *SysUserService) QuerySysUser(sysUser *models.SysUser, pageNum, pageSize int) PageInfo {
	tables, total := service.SysUserRepository.SelectSysUser(sysUser, pageNum, pageSize)
	return toPageInfo(pageNum, pageSize, total, tables)
}

// QuerySysUserById 通过ID查询系统用户
func (service *SysUserService) QuerySysUserById(id int) models.SysUser {
	return service.SysUserRepository.SelectSysUserById(id)
}

// QuerySysUserByUsername 通过用户名查找用户
func (service *SysUserService) QuerySysUserByUsername(username string) models.SysUser {
	return service.SysUserRepository.SelectSysUserByUsername(username)
}
